package com.syh.niceweibo;

import com.syh.niceweibo.common.Constants;
import com.syh.niceweibo.common.LogUtils;
import com.syh.niceweibo.common.Utils;
import com.weibo.net.AccessToken;
import com.weibo.net.DialogError;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;
import com.weibo.net.WeiboLoginListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class LoginActivity extends Activity{
	public final static String TAG="LoginActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_login);
		
		findViewById(R.id.login).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				login(LoginActivity.this);
			}
		});
	}
	
	
	private void login(Activity activity) {
		if (Utils.isConnected(activity)) {
			Weibo weibo = Weibo.getInstance();
			weibo.setupConsumerConfig(Constants.SINA_KEY, Constants.SINA_SECRET);
			// Oauth2.0
			// 隐式授权认证方式
			// 此处回调页内容应该替换为与appkey对应的应用回调页
			weibo.setRedirectUrl("https://api.weibo.com/oauth2/default.html");

			// 对应的应用回调页可在开发者登陆新浪微博开发平台之后，
			// 进入我的应用--应用详情--应用信息--高级信息--授权设置--应用回调页进行设置和查看，
			// 应用回调页不可为空
			weibo.authorize(activity, mWeiboLoginListener);
		} else {
			Toast.makeText(activity, R.string.net_disconnected, 0).show();
		}
	}
	
	private WeiboLoginListener mWeiboLoginListener = new WeiboLoginListener() {

		@Override
		public void onWeiboException(WeiboException e) {
			if (LogUtils.DEBUG)
				LogUtils.d(TAG, "authorize onWeiboException" + e.toString());
			Toast.makeText(getApplicationContext(), R.string.login_faild, 0)
					.show();
		}

		@Override
		public void onError(DialogError e) {
			if (LogUtils.DEBUG)
				LogUtils.d(TAG, "authorize onError" + e.toString());
			Toast.makeText(getApplicationContext(), R.string.login_faild, 0)
					.show();
		}

		@Override
		public void onComplete(Bundle values) {
			final String token = values.getString(Weibo.TOKEN);
			final String expires_in = values.getString(Weibo.EXPIRES);
			final String uid = values.getString(Weibo.UID);
			if (LogUtils.DEBUG)
				LogUtils.d(TAG, "authorize onComplete uid:" + uid + " token:"
						+ token + " expires_in:" + expires_in);
			String oldUid = Utils.getUID(getApplicationContext());
			if (oldUid != null && !oldUid.equals(uid)) {
				// todo clear data:DB,List,Preference
				
			}

			AccessToken accessToken = new AccessToken(token,
					Constants.SINA_SECRET);
			accessToken.setExpiresIn(expires_in);
			Weibo.getInstance().setAccessToken(accessToken);

			Utils.storeWeiboAccessInfo(getApplicationContext(), token,
					expires_in, uid);

			Toast.makeText(getApplicationContext(), R.string.login_success, 0).show();
			startActivity(new Intent(LoginActivity.this, MainActivity.class));
		}

		@Override
		public void onCancel() {
			if (LogUtils.DEBUG)
				LogUtils.d(TAG, "authorize onCancel");
		}

	};
}
